package com.review.Test.ConcurrentUtil;

import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

public class UseSemaphore {
	
	public static void main(String[] args) {
		
		final Semaphore semaphore = new Semaphore(5);
		final AtomicInteger num = new AtomicInteger();
		
		for (int i = 0; i < 20; i++) {
			Thread thread = new Thread(new Runnable() {
				
				@Override
				public void run() {
					try {
						semaphore.acquire();
						System.out.println(num.incrementAndGet() + "  run...");
						Thread.sleep(5000);
						semaphore.release();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			});
			
			thread.start();
		}
		
	}

}
